package com.yozinga.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;

public class ToolKit {

	Context mContext;
	SharedData mSharedData;
	public ToolKit(Context context) {
		mContext = context;
		mSharedData = new SharedData(mContext);
	}
	public static ProgressDialog dialog;

	/** Checks whether Internet connection is available */
	public static boolean isInternetAvailable(Context context) {
		/**
		 * This context will be used in builduseragent method
		 */
		if(context!=null){
			ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (cm.getActiveNetworkInfo() != null)
				return cm.getActiveNetworkInfo().isConnectedOrConnecting();
			else
				return false;
		}
		return false;
		
	}
	/**
	 * Device Time Format
	 * 
	 * @return
	 */
	public static SimpleDateFormat timeFormat(Context context) {
		if (android.text.format.DateFormat.is24HourFormat(context)) {
			return new SimpleDateFormat("H:mm");
		} else {
			return new SimpleDateFormat("h:mm a");
		}
	}
	/**
	 * Used to display progress bar showing logging process
	 */
	public static void startProgress(Context context, int message) {
		if(context!=null){
			String loadingMessage = context.getString(message);
			dialog = ProgressDialog.show(context, "", loadingMessage, true);
		}
		
	}

	/**
	 * Used to stop progress bar
	 */
	public static void stopprogess() {
		if (dialog != null) {
			dialog.dismiss();
		}
	}

	public static double distance(double lat1, double lon1, double lat2, double lon2, String unit){

		double theta = lon1 - lon2;
		double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
		dist = Math.acos(dist);
		dist = rad2deg(dist);
		dist = dist * 60 * 1.1515;
		if (unit == "K") {
			dist = dist * 1.609344;
		} else if (unit == "N") {
			dist = dist * 0.8684;
		}

		return (dist);
	}

	public static String formatNumber(String value) {
		NumberFormat myFormatter = DecimalFormat.getInstance();
		myFormatter.setMaximumFractionDigits(2);
		myFormatter.setMinimumFractionDigits(2);
		String mValue = myFormatter.format(Double.parseDouble(value));
		return mValue;
	}
	private static double deg2rad(double deg) {
		return (deg * Math.PI / 180.0);
	}


	private  static double rad2deg(double rad) {
		return (rad * 180 / Math.PI);
	}

	/*public static void main(String[] args) {

		System.out.println(A.distance(32.9697, -96.80322, 29.46786, -98.53506, "M") + " Miles\n");
		System.out.println(A.distance(32.9697, -96.80322, 29.46786, -98.53506, "K") + " Kilometers\n");
		System.out.println(A.distance(32.9697, -96.80322, 29.46786, -98.53506, "N") + " Nautical Miles\n");
	}*/

	public String categoryServiceCall(){

		String categoryUrl;
		// public static final String CATEGORY_LIST = BASE_URL + "/merchant/ListMerchantsByLocation?radius=100&latitude=27.175009&longitude=78.041849&buisenessType=";
		categoryUrl = AppData.CATEGORY_LIST+mSharedData.getRadius()+"&latitude="+mSharedData.getLatitude()+"&longitude="+mSharedData.getLongitude()+"&buisenessType=";
		return categoryUrl;
	}
}


